import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store=new Vuex.Store({
//全局变量
state:{
    dialogFormVisible: false,
    name:'',
    isLogined:false,
    user:'',
},
// 修改全局变量必须通过mutations中的方法
// mutations只能采用同步方法
mutations:{
      setDialogFormVisible(state,dialogFormVisible){
        state.dialogFormVisible=dialogFormVisible
      },
      getName(state) {
        return state.name
      },
      setName(state, name) {
        state.name = name
      },
      getUser(state) {
        return state.user
      },
      setUser(state, user) {
        state.user = user
      },
      getIsLogined(state) {
        return state.isLogined
      },
      setIsLogined(state, isLogined) {
        state.isLogined = isLogined
      },
      logout(state){
        state.user=undefined;
        state.isLogined=false;
      }


},
// 异步方法用actions
// actions不能直接修改全局变量，需要调用commit方法来触发mutation中的方法
  actions: {
    login (context, user,isLogined) {
        context.commit('setUser',user)
        context.commit('setIsLogined',isLogined)
    },
    logout (context) {
        context.commit('setUser',undefined)
        context.commit('setIsLogined',false)
    }
  },

})
export default store
